package com.jiajia.study.netty.chapter02;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

public class NettyClient02 {
    public static void main(String[] args) throws InterruptedException {

        Bootstrap bootstrap = new Bootstrap();

        bootstrap.channel(NioSocketChannel.class);
        bootstrap.group(new NioEventLoopGroup());
        bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                nioSocketChannel.pipeline().addLast(new StringEncoder());
            }
        });
        /**
         * 连接是异步的
         */
        ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(8888));
        /**
         * 阻塞：等待连接成功
         */
        channelFuture.sync();

        Channel channel = channelFuture.channel();
        channel.writeAndFlush("new message : hi netty ...");


    }
}
